<?php

namespace App\Models;

class WechatSet extends BaseModel
{
    protected $table = "wechat_set";

    protected $redisTable = "wechat_set";


    protected $rules = [
        'admin_id' => 'required|unique:wechat_set,admin_id',
    ];

    protected $message = [
        'admin_id.required' => '用户ID获取失败',
        'admin_id.unique' => '该商户信息已存在',
    ];

    public function addOrUpdate()
    {
        $data['admin_id'] = plat_admin_id();
        $data['wx_app_id'] = trim(request('wx_app_id'));
        $data['wx_app_secret'] = trim(request('wx_app_secret'));
        $data['wx_aes_key'] = trim(request('wx_aes_key'));
        $data['mch_id'] = trim(request('mch_id'));
        $data['mch_key'] = trim(request('mch_key'));
        $data['xcx_app_id'] = trim(request('xcx_app_id'));
        $data['xcx_app_secret'] = trim(request('xcx_app_secret'));
        $data['xcx_notice_send_id'] = trim(request('xcx_notice_send_id'));
        $data['xcx_notice_complete_id'] = trim(request('xcx_notice_complete_id'));
        if (request('id')) {
            $data['id'] = request('id');
            $this->rules['admin_id'] .= ',' . $data['id']; //唯一字段验证,在更新时排除当前id
        }

        $this->_start();
        $res = $this->_save($data);
        if (!$res) {
            $this->_rollBack();
            $this->error = '保存失败！';
            return false;
        }
        if(request('pics') && count(request('pics')) && $data['wx_app_id']) {
            $picutreModel = new  Picture();
            $picutreModel->config['maxNum'] = 1;
            $picutreModel->config['cut'] = 0;
            $picutreModel->config['cutSize'] = '0';
            $picutreModel->config['cut_Mini_Size'] = 0;
            $res = $picutreModel->upload($data['wx_app_id'],'/uploads/share/');
            if(!$res) {
                $this->_rollBack();
                $this->error = '图片上传失败';
                return false;
            }
        }

        $this->_commit();

        $this->_redisGenerate(1);
        return $res;
    }

    public function getWeChatSetList()
    {
        $keyword = request('keyword');
        $where = [];
        if ($keyword) {
            $rows = $this->_getJoinPageList('admin', $where, '*', ['id' => 'desc'],10,5,'id,nickname',['nickname' => ['like', '%' . $keyword . '%']]);
        } else {
            $rows = $this->_getJoinPageList('admin', $where, '*', ['id' => 'desc'],10,5,'id,nickname');
        }
        $yes = true;
        $no = false;
        if(count($rows['data'])) {
            foreach ($rows['data'] as $key=>$value) {
                $rows['data'][$key]['nickname'] = $value['admin']['nickname'];
                $rows['data'][$key]['wx_app_id'] = $value['wx_app_id'] ? $yes : $no;
                $rows['data'][$key]['wx_app_secret'] = $value['wx_app_secret'] ? $yes : $no;
                $rows['data'][$key]['mch_id'] = $value['mch_id'] ? $yes : $no;
                $rows['data'][$key]['mch_key'] = $value['mch_key'] ? $yes : $no;
                $rows['data'][$key]['xcx_app_id'] = $value['xcx_app_id'] ? $yes : $no;
                $rows['data'][$key]['xcx_app_secret'] = $value['xcx_app_secret'] ? $yes : $no;
                unset($rows['data'][$key]['admin']);
            }
        }
        return $rows;
    }

    public function _redisGetOneByAid($aid)
    {
        $table = $this->redisTable;
        $key = $aid;
        if ($this->redis->hexists($table, $key)) {
            $res = $this->redis->hmget($table, $key);
            if ($res) {
                return unserialize($res[0]);
            }
        }
        $res = $this->_getOne(['admin_id' => $aid]);
        if ($res) {
            $res = $res->toArray();
        }
        $this->redis->hmset($table, [$key => serialize($res)]);
        return $res;
    }

    public function _redisGenerate($aid)
    {
        $table = $this->redisTable;
        $key = $aid;
        $this->redis->hdel($table, $key);
    }

    public function qrDownHandle($aid, $sid = 0)
    {
        $type = request('type');
        $ext = (new Picture())->config['saveExt'];
        if($type == 'example') {
            $fileName = public_path().'/admin/images/qr_img_default.jpg';
        } else {
            $info = $this->_getOne(['admin_id' => $aid]);
            if(!$info || !$info['wx_app_id']) {
                return false;
            }
            if ($sid){
                $fileName = public_path().'/uploads/qrImg/'.$info['wx_app_id'].'_qr'.'_'.$sid.'.'.$ext;
            } else {
                $fileName = public_path().'/uploads/qrImg/'.$info['wx_app_id'].'_qr'.'.'.$ext;
            }

            if(!file_exists($fileName)) {
                return false;
            }
        }

        return $fileName;
    }
}
